package com.liry.zk;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * 原生客户端操作测试
 *
 * @author ALI
 * @since 2022/12/22
 */
public class TestZk {

    private static final String CONNECT_STR = "192.168.17.128:2181";

    private static final int TIME_OUT = 30000;

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {

        final CountDownLatch latch = new CountDownLatch(1);
        Watcher startWatcher = watchedEvent -> {
            if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) {
                System.out.println("建立连接");
                latch.countDown();
            }
            String path = watchedEvent.getPath();
            System.out.println("path:" + path);
        };

        ZooKeeper zookeeper = new ZooKeeper(CONNECT_STR, TIME_OUT, startWatcher);
        // zookeeper内部有开启子线程，所以这里通过countlatch锁阻止主线程的执行
        latch.await();

        // 判断节点/test是否存在
        Stat exists = zookeeper.exists("/test", false);
        //        // 当节点不存在时exits=null
        //        System.out.println(exists);
        //
        //
        //        // 获取节点/test数据
        //        // 查询节点，需要一个stat来承载节点信息
        Stat stat = new Stat();
        byte[] data = zookeeper.getData("/test", false, stat);
        System.out.println("节点/test数据：");
        System.out.println(new String(data));
        //
        //
        //        // 修改节点/test数据
        //        // version=-1表示不检查版本
        zookeeper.setData("/test", "false".getBytes(), -1);
        data = zookeeper.getData("/test", false, stat);
        System.out.println("节点/test修改后的数据：");
        System.out.println(new String(data));
        //
        //        // 创建节点/test/t4
        ZkData d = new ZkData();
        d.setKey("ddd");
        d.setValue("vvv");
        //
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValueAsBytes(d);

        String s = zookeeper.create("/test/tttt", objectMapper.writeValueAsBytes(d), ZooDefs.Ids
                                        .OPEN_ACL_UNSAFE,
                                    CreateMode.PERSISTENT);


        data = zookeeper.getData("/test/tttt", false, stat);
        System.out.println("节点/test/tttt的数据：");
        System.out.println(new String(data));


        Watcher dataWatch = event -> {
            if (event.getType() == Watcher.Event.EventType.NodeDataChanged) {
                System.out.println("数据更新");
                System.out.println(event);
                // 重新获取一次数据getData
                try {
                    byte[] data1 = zookeeper.getData("/test", false, null);
                    System.out.println(new String(data1));
                } catch (KeeperException | InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        byte[] data1 = zookeeper.getData("/test", dataWatch, null);
        System.out.println();

        TimeUnit.SECONDS.sleep(600);

    }


}
